package com.maven.front.controller.frontQuestions;

import com.maven.front.aop.MyLog;
import com.maven.front.bean.EduQuestions;
import com.maven.front.bean.EduQuestionsComment;
import com.maven.front.bean.Result;
import com.maven.front.dto.MyEduQuesstionDto;
import com.maven.front.service.frontQuestions.QuestionsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("user/questions")
@Slf4j
public class QuestionsController {

    @Autowired
    private QuestionsService questionsService;
//   @RequestMapping("list")
//    public Result list(EduQuestionsDto eduQuestionsDto){
//       log.info(eduQuestionsDto.getBrowseCount()+"***************************"+eduQuestionsDto.getReplyCount());
//       List<EduQuestionsDto> list= questionsService.list(eduQuestionsDto);
//       if(CollectionUtils.isEmpty(list)){
//           return new Result(400,"查询失败",null);
//       }else {
//           return  new Result(200,"查询成功",list);
//       }
//    }
//    @RequestMapping("iflist")
//    public  Result ifist(EduQuestionsDto eduQuestionsDto){
//       eduQuestionsDto.setAddTime(new Date());
//        List<EduQuestionsDto> list= questionsService.list(eduQuestionsDto);
//        if(CollectionUtils.isEmpty(list)){
//            return new Result(400,"查询失败",null);
//        }else {
//            return  new Result(200,"查询成功",list);
//        }
//    }
//    @GetMapping("select")
//    public  Result select(){
//       List<EduQuestionsTag> list=questionsService.select();
//       if(list==null){
//           return new Result(400,"查询失败",null);
//       }
//        return  new Result(200,"查询成功",list);
//    }
//
//    @PostMapping("info")
//    public  Result changeOne(Integer id){
//        EduQuestionsDto ed=questionsService.changeOne(id);
//        log.info(id+"******************");
//        if(ObjectUtils.isEmpty(ed)){
//            return new Result(400,"查询失败",null);
//        }
//        return  new Result(200,"查询成功",ed);
//    }
    @MyLog("问答页面显示")
    @GetMapping("show")
    public Result showQuestions(MyEduQuesstionDto dto){
        log.info("*******************"+dto.toString());
        List<MyEduQuesstionDto> quesstionDtos = questionsService.selectAllDescByAddTimeOrView(dto);
        log.info("*****************"+quesstionDtos.toString());
        if(CollectionUtils.isEmpty(quesstionDtos)){
            return new Result(227,"查询无数据");
        }
        return new Result(200,"成功",quesstionDtos);
    }

    @MyLog("问答详细页面")
    @GetMapping("showSingle")
    public Result showSingle(Integer id){
        Map<String,Object> map = questionsService.getById(id);
        if(CollectionUtils.isEmpty(map)){
            return new Result(227,"查询无数据");
        }
        return new Result(200,"成功",map);
    }

    @MyLog("问答页面添加回复")
    @PostMapping("save/comment")
    public Result saveComment(EduQuestionsComment comment){
        Date date = new Date();
        comment.setAddTime(date);
        int num = questionsService.saveComment(comment);
        if(num>0){
            return new Result(200,"成功");
        }
        return new Result(255,"添加失败");
    }

    @MyLog("问答点赞")
    @PostMapping("addQuestionPraiseCount")
    public Result addPraiseCount(EduQuestions newQuestion){
        int num = questionsService.addQuestionPraiseCount(newQuestion);
        if(num>0){
            return new Result(200,"成功");
        }
        return new Result(255,"点赞失败");
    }

    @MyLog("评论点赞")
    @PostMapping("addCommentPraiseCount")
    public Result addCommentPraiseCount(EduQuestionsComment newComment){
        int num = questionsService.addCommentPraiseCount(newComment);
        if(num>0){
            return new Result(200,"成功");
        }
        return new Result(255,"点赞失败");
    }

    @MyLog("添加提问")
    @PostMapping("addQuestions")
    public Result addQuestions(MyEduQuesstionDto dto){
        int num = questionsService.saveQuestion(dto);
        if(ObjectUtils.isEmpty(num)){
            return new Result(209,"新增失败");
        }
        return new Result(200,"成功");
    }

    @MyLog("采纳最近答案")
    @PostMapping("getBestResult")
    public Result updateQuestions(EduQuestionsComment comment){
        int num = questionsService.updateStatus(comment);
        if(num>0){
            return new Result(200,"修改成功");
        }
        return new Result(257,"修改失败");
    }
}
